package com.jinboe.comm.dao.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.jinboe.comm.dao.BaseDao;
import com.jinboe.comm.dto.BaseDto;
import com.jinboe.comm.util.BeanUtils;
import com.jinboe.comm.util.Constants;

public class BaseDaoImpl extends JdbcDaoSupport implements BaseDao {
	
	//数据记录数sql属性
	protected String NoteCountName="noteCount";
	
	
	public int updateIsValid(String orderItemId,Integer isValid,String remark){
		return 0;
	}
	public String insertBaseInfo(){
		String id=getUUID();
		String insertBaseInfoSql="Insert into YB_ORDERITEM (MAININFOID,CREATETIME,ISVALID,UPDATETIME)" +
			" values (?,sysdate,?,sysdate)";
		getJdbcTemplate().update(insertBaseInfoSql,new Object[] {id,Constants.VALUE_YESORNO_YES});
		return id;
	}
	/**
	 * 查询语句
	 * @param sql
	 * @param start
	 * @param end
	 * @return
	 */
	protected String createPageSql(String sql,int start,int end){
		return "SELECT * FROM ( SELECT A.*, ROWNUM RN FROM ("+sql+") A ) WHERE RN BETWEEN "+start+" AND "+end;
	}
	protected String createBaseWhereConSql(String queryListSql,Map conMap){
		String sql = queryListSql ;		
		Integer isValid=(Integer)conMap.get(Constants.KET_NAME_ISVALID);
		String mainInfoId=(String)conMap.get(Constants.KET_NAME_MAININFOID);
		if(StringUtils.isNotBlank(mainInfoId)){
			sql+=" and  baseInfo.MAININFOID='"+mainInfoId.trim()+"' ";
		}
		if(isValid!=null){
			sql+=" and  baseInfo.ISVALID="+isValid+" ";
		}else{
			//默认只取有效数据
			sql+=" and  baseInfo.ISVALID="+Constants.VALUE_YESORNO_YES+" ";
		}
		
		return sql;
	}
	/**
	 * 通用创建生成Dto方法 SQL中列与Dto中属性无法对应的不适用
	 * @param baseDto
	 * @param queryIteratorMap
	 * @return
	 */
	protected void createDto(BaseDto baseDto,Map queryIteratorMap ){
		try {
			List<Map> propertieList=BeanUtils.getBeanProperties(baseDto);
			for(Map pMap:propertieList){
				Set keySet=pMap.keySet();
				Iterator it=keySet.iterator();
				String propertyName=null;
				if(it.hasNext()){
					propertyName=(String)it.next();
				}
				if(queryIteratorMap.get(propertyName)!=null&&queryIteratorMap.get(propertyName)!=null){
					BeanUtils.setBeanProperties(baseDto, propertyName, queryIteratorMap.get(propertyName), (Class)queryIteratorMap.get(propertyName));
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		   
	}
	
	protected String getUUID(){
		 UUID uuid = UUID.randomUUID();
	     return uuid.toString().replace("-", "");

	}
	
	
	
}